<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>无爱咖啡</span>
        </div>
      </template>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <!-- 手机号输入项 -->
        <el-form-item>
          <el-input v-model="loginForm.phone" placeholder="请输入手机号" class="login-input"></el-input>
        </el-form-item>
        <!-- 密码输入项 -->
        <el-form-item>
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码" show-password class="login-input"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(loginFormRef)" class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="register-link">
        <span>还没有账户？</span>
        <span class="register-text" @click="goToRegister">立即注册</span>
      </div>
      <!-- 返回首页按钮 -->
      <div class="back-home">
        <el-button link @click="goToHome">
          ← 返回首页
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { login } from '@/api/login.js'

const router = useRouter()
const loginFormRef = ref(null)

const loginForm = reactive({
  phone: '',
  password: ''
})

const loginRules = reactive({
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
})

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      try {
        console.log('Sending login request with:', loginForm)
        const response = await login(loginForm)
        console.log('Login response:', response)
        
        const token = response.data
        if (!token) {
          throw new Error('No token in response')
        }
        localStorage.setItem('authToken', token)
        console.log('Token stored in localStorage:', localStorage.getItem('authToken'))
        
        ElMessage.success('登录成功！')
        router.push('/me')
      } catch (error) {
        console.error('Login error:', error)
        ElMessage.error(error.response?.message || '登录失败，账号可能不存在或被禁用')
      }
    } else {
      ElMessage.error('请检查输入信息！')
    }
  })
}

const goToRegister = () => {
  router.push('/register') // 确保你有 /register 页面
}

const goToHome = () => {
  router.push('/')
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
  padding: 20px;
  box-sizing: border-box;
}

.card-header {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 24px;
  color: #2c3e50;
  letter-spacing: 0.5px;
}

.login-form {
  display: flex;
  flex-direction: column;
  align-items: center; /* 使表单内容居中 */
}

.login-input {
  width: 90%; /* 拉长到接近整个界面 */
  margin-bottom: 20px; /* 增加输入框之间的间距 */
  min-width: 300px; /* 确保在小屏幕设备上有最小宽度 */
  text-align: center; /* 居中文本输入框中的文本 */
}

.login-button {
  width: 95%; /* 拉长到接近整个界面 */
  min-width: 300px; /* 确保在小屏幕设备上有最小宽度 */
  background-color: #5c6bc0;
  color: white;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  padding: 12px 20px;
  transition: background-color 0.3s ease;
  margin-top: 20px; /* 增加按钮与输入框之间的间距 */
}
.login-button:hover {
  background-color: #3949ab;
}

.register-link {
  display: flex;
  justify-content: center; /* 使内容居中显示 */
  align-items: center; /* 垂直居中 */
  margin-top: 20px;
  font-size: 14px;
  color: #666;
}

.register-link span {
  margin-right: 10px; /* 增加 span 和文本之间的间距 */
}

.register-text {
  color: #5c6bc0; /* 设置字体颜色为蓝色 */
  cursor: pointer; /* 添加鼠标悬停效果 */
  transition: color 0.3s ease;
}

.register-text:hover {
  color: #3949ab; /* 鼠标悬停时字体颜色变为更深的蓝色 */
}

.back-home {
  position: absolute;
  top: 20px;
  left: 40px;
  font-size: 14px;
}

.back-home .el-button {
  padding: 0;
  color: #5c6bc0;
  font-weight: 500;
}

.back-home .el-button:hover {
  color: #3949ab;
}

.login-card {
  position: relative;
  width: 100%;
  max-width: 500px; /* 限制卡片的最大宽度 */
  padding: 40px;     /* 增加 padding 提升内部空间感 */
  border-radius: 16px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
  transition: all 0.3s ease-in-out;
}

/* 自定义输入框占位符样式 */
::v-deep .el-input__inner::placeholder {
  text-align: center;
}
</style>
